package com.stutter.myproject.server;


import com.stutter.myproject.client.GreetingService;
import com.stutter.myproject.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 10 characters long");
		}

		//String serverInfo = getServletContext().getServerInfo();
		//String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		//userAgent = escapeHtml(userAgent);

		
		 // String strLine;
		  /** Declaration of array string variable which will hold words from splitted line of input */
		  String[] arrstr;
		  /** Linecount variable represents on which line number words are repeated */
		  Integer linecount=1;
		  String[] newlineArr;
		  String strline;
		  String outputstr="";
		  newlineArr=input.split("\n");
				  for(int nl =0; nl < newlineArr.length ; nl++) 
				  {strline=" " + newlineArr[nl].replace("\n"," ") + " -";
				  strline=strline.replace("\n"," ");
				  strline=strline.replace(","," ");
				  strline=strline.replace("."," ");	
				  strline=strline.replace("!"," ");
				  strline=strline.replace("?"," ");
				  strline=strline.replace("  "," ");
				/** This is splitting words by space */	  
				  arrstr=strline.split(" ");
					  for(int i =0; i < arrstr.length ; i++)
					  {
						  if (i>1)
						  {
							/** If we passed first word in line then we compare second against previous*/
							if (arrstr[i].equalsIgnoreCase(arrstr[i-1]))
							{
								/** @return This is actual output with linecount and repeated words if matched */ 
								outputstr=outputstr + "Repeated word on line " + linecount.toString() + ":" + arrstr[i-1] + " " + arrstr[i] + "<BR>";
							}							
						  }							 				   
					  }
					  /** @return When reading next line we incrementing linecount by 1*/
					  linecount+=1;
				  }
		  /**return done*/
				  return outputstr;
		     }

        

		//return "Hello, " + input + "!<br><br>I am running " + serverInfo
		//		+ ".<br><br>It looks like you are using:<br>" + userAgent;
	

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */

	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
}
